<?php
class RouteManager {
	private $routes = array();
	private $loginRoute;
	
	function registerRoute($route) {
        $this->routes[] = $route;    
    }
    
    function registerLoginRoute($route){
    	$this->login = $route;
    	$this->registerRoute($route);
    }
    
    // Gets the Route for the current request
    function getCurrentRoute() {
        $santitizedUrl = '';                                 
        if (!empty($_GET['route'])) {
            $santitizedUrl = trim($_GET['route']);
        }
        
        return $this->getMatchingRoute($santitizedUrl);
    }    
    
    // Gets the registered route that matches the specified 
    //   sanitized url
    function getMatchingRoute($santitizedUrl) {
        
        foreach ($this->routes as &$route) {            
            if ($route->isMatch($santitizedUrl)) {
                return $route;            
            }
        }
        
        return null; 
    } 
    
    // Calls the controller's action that the matching route points to
    function execute($url) {
        $santitizedUrl = '';

        if (!empty($url)) {
            $santitizedUrl = trim($url);
        }

        $route = $this->getMatchingRoute($santitizedUrl);

        if ($route != null) {
            $controllerName = $route->getControllerName();
            $controller = new $controllerName;
            $action = $route->parseMethod($santitizedUrl);

            $vars = array();
            foreach ($_GET as $varname => $varvalue) {
                $vars[$varname] = $varvalue;
            }

            return array($controller, $action, $vars);
        } else {
            // If we've entered this else section, we've effectively been unable to match
            //   the specified url to a registered route. Normally we would throw a 404 error
        }
    }
}
?>
